let user = JSON.parse(localStorage.getItem("USER_LOGIN"));
$(() => {
    //获取用户
    getUser();
    //建立链接
    WebSocketTest();
    //改变用户名的颜色
    changeColor
});

//从本地存储获取用户信息
function getUser() {
    let userName = user.userName;
    $("#userName").append(userName);
}

//websocket

function WebSocketTest() {
    if ("WebSocket" in window) {
        //alert("您的浏览器支持 WebSocket!");

        // 打开一个 web socket
        let ws = new WebSocket("ws://www.hefeixyh.com/chat");

        ws.onopen = function () {
            // Web Socket 已连接上，使用 send() 方法发送数据
            let person = {
                "userName": user.userName,
                "msg": "进入聊天室"
            };
            ws.send(JSON.stringify(person));
        };

        ws.onmessage = function (evt) {
            console.log(evt);
            //要点他就要先转化一下啊,不然就是undefined
            let received_msg = JSON.parse(evt.data);
            //谁来了
            $('#who').html(received_msg.userName);
            if (received_msg.msg.indexOf("进入聊天室")!==-1){
                $('#jc').html("进入聊天室")
            } else if (received_msg.msg.indexOf("退出聊天室")!==-1){
                $('#jc').html("退出聊天室")
            } else {
                $('#jc').html("进入聊天室")
            }

            //来了多少人
            $('#count').html(received_msg.count);
            //来的人说了啥
            if (!received_msg.userName) return;
            if (received_msg.msg.indexOf('聊天室') == -1) {
                let time=$('<span id="time">'+getTime()+'</span>');
                let li=$("<li class='list-group-item'>" + received_msg.userName + "说:" + received_msg.msg +"</li>")
                li.append(time);
                $('.list-group').prepend(li);
           }

        };
        //关了是什么样
        ws.onclose = function () {
            // 关闭 websocket
            alert("连接已关闭...");
        };
        //坐稳了 我要发送消息了
        $('.send').click(function () {
            let msg = $("#message").val();
            //把用户和发言发给服务器
            if (msg=='') return;
            let person = {
                "userName": user.userName,
                "msg": msg
            };
            ws.send(JSON.stringify(person));
            //清空输入框
            $("#message").val('');


        });
        //回车发送
        $('#message').keydown(function(event){
            console.log(event);
            if (event.which==13){
                let msg = $("#message").val();
                //把用户和发言发给服务器
                if (msg=='') return;
                let person = {
                    "userName": user.userName,
                    "msg": msg
                };
                ws.send(JSON.stringify(person));
                //清空输入框
                $("#message").val('');
            }
        });
        //8、窗口关闭前执行的动作
        window.onbeforeunload = function () {
            let person = {
                "userName": user.userName,
                "msg": "退出聊天室"
            };
            ws.send(JSON.stringify(person));
            //断开服务器链接
            ws.close();
        };
    } else {
        // 浏览器不支持 WebSocket
        alert("您的浏览器不支持 WebSocket!");
    }
}
//改变颜色
let n=0;

 let  changeColor=setInterval(()=> {
    n++;
    if (n%2){
        $('#userName').css({'color':'red','fontSize':'30px'})
    } else {
        $('#userName').css({'color':'orange','fontSize':'30px'})
    }
},100);
//时间戳
function getTime() {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let monthDay = date.getDate();
    let weekDay = date.getDay();
    let hour = date.getHours();
    let min = date.getMinutes();
    let second = date.getSeconds();
    let milSecond = date.getMilliseconds();
    let nowTime = year + "-"
        + (month < 10 ? "0" + month : month) + "-"
        + (monthDay < 10 ? "0" + monthDay : monthDay) + "  "
        //+ weekDay + " "
        + (hour < 10 ? "0" + hour : hour) + ":"
        + (min < 10 ? "0" + min : min);
    //+ ":" + (second < 10 ? "0" + second : second)
    //+ ":" + milSecond;
    return nowTime
}